//
//  Request.m
//  Doctor
//
//  Created by 阳光 on 16/9/14.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "Request.h"
#import "LoginVC.h"


@implementation Request

+(instancetype)shareInstance
{
    static Request *request=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        request=[[Request alloc]init];
    });
    return request;
}


// POST
+(void)PostCallBack:(NSString *)url parameter:(NSDictionary *)paramDic success:(SuccessResult)success failure:(FailureResult)failure
{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];

    manager.responseSerializer=[AFJSONResponseSerializer new];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    //NSLog(@"postUrl----- :%@", url);
    
    [manager POST:url parameters:paramDic success:^(NSURLSessionDataTask *task, id responseObject){
        
        //NSLog(@"responseObject000 :%@", responseObject);
        
        if ([responseObject[@"status"]  isEqualToString:@"403"])
        {
            
            [[[Request alloc] init] gotoAlertMessageBy:@"您的帐号在其他设备登录"];
            [[[Request alloc] init] gotoLoginAction];
            return;
            
        } else if ([responseObject[@"status"]  isEqualToString:@"404"]){
            
            
            [[[Request alloc] init] gotoAlertMessageBy:responseObject[@"message"]];
            return;
        }
        
        success(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        if (error!=nil)
        {
            [[[Request alloc] init] gotoAlertMessageBy:@"服务器开小差了"];
            failure(error);
        }
    }];
}



     
//.
- (void)gotoLoginAction{

    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate clearUserData];
    
    delegate.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[LoginVC new]];
}

-(void)gotoAlertMessageBy:(NSString *)message
{
    
    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate showMessageByMessage:message];
    
}

@end
